package com.excilys.service;

import javax.persistence.NoResultException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.excilys.model.User;

public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

	@Autowired
	private UserService myUserService;

	@Override
	public UserDetails loadUserByUsername(String userLogin) throws UsernameNotFoundException, DataAccessException {

		User u;
		try {
			u = myUserService.getUserByLogin(userLogin);
		} catch (NoResultException e) {
			throw new UsernameNotFoundException("UserNotFound");
		}
		return u;
	}

}
